package com.hl.sfzy.service.impl;

import com.hl.sfzy.entity.SystemUser;
import com.hl.sfzy.mapper.SystemUserMapper;
import com.hl.sfzy.service.SystemUserService;
import com.hl.sfzy.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class SystemUserServiceImpl implements SystemUserService {
    @Autowired
    private SystemUserMapper systemUserMapper;

    @Override
    public int add(SystemUser systemUser) {
        String s = systemUser.getSystemUserUsername() + systemUser.getSystemUserPassword();
        systemUser.setSystemUserPassword(s);
//        System.out.println(systemUser.getSystemUserPassword());
        systemUser.setToken(UUID.randomUUID().toString().replaceAll("-","").substring(0,6));
        return systemUserMapper.insert(systemUser);
    }

    @Override
    public int remove(Long systemUserId) {
        return systemUserMapper.deleteByPrimaryKey(systemUserId);
    }

    @Override
    public int edit(SystemUser systemUser) {
        return systemUserMapper.updateByPrimaryKey(systemUser);
    }

    @Override
    public SystemUser findById(Long systemUserId) {
        return systemUserMapper.selectByPrimaryKey(systemUserId);
    }

    @Override
    public SystemUser login(String username, String password) {
        SystemUser systemUser = systemUserMapper.login(username, password);
        if(systemUser!=null&&systemUser.getSystemUserPassword().equalsIgnoreCase(MD5Util.encode(username+password))) {
           return systemUser;
        }else{
            return null;
        }

    }
}


